﻿@{
    Layout = "~/Views/Shared/_Layout_RTU_well.cshtml";
    ViewBag.Title = "遥调";
}
<div class="xds-no-breadcrumb">
    <div class="full-height">
        <div class="full-height-scroll white-bg border-left">
            <div class="row border-bottom page-heading" style="padding:0 20px 10px 20px">
                <div class="row">
                    <div class="col-sm-6">
                        <h2 style="float:left">遥调控制</h2>
                    </div>
                    <div class="col-sm-6">
                        <label class="font-noraml"></label>
                        <div class="form-group">
                            @*<select id="Options" style="margin-top:4px">
                                    <option></option>
                                </select>*@
                            <button type="button" onclick="showlist()" class="btn btn-primary" style="float:right;">显示/隐藏</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="checkboxlist" style="padding-left:5%;display:none;background-color:ghostwhite"></div>
            <div class=" wrapper-content animated fadeInRight">
                <div class="row">
                    <div id="fixedsensor" style="padding:15px">
                        @*<div class="sidebar-message">
                                <div class="panel panel-primary">
                                    <div class="panel-heading">
                                        Inline form
                                    </div>
                                    <div class="panel-body">
                                        <div style="float:left">
                                            <button type="button" class="btn btn-outline btn-primary">产品配置信息</button>
                                        </div>
                                        <div style="float:left;margin:8px">
                                            电池电量剩余百分比：
                                        </div>
                                        <div class="progress" style="width:50%;float:left;margin:7px 0 7px 0">
                                            <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em;">
                                                0%
                                            </div>
                                        </div>
                                        <div style="float:right">
                                            <button type="button" class="btn btn-outline btn-warning">遥调</button>
                                        </div>
                                    </div>
                                    <table class="table table-hover">
                                        <thead><tr><th>参数名</th><th>单位</th><th>取值范围</th><th>当前参数值</th><th>遥调值</th><th>其他</th></tr></thead>
                                        <tbody><tr><td>参数名</td><td>单位</td><td>取值范围</td><td>当前参数值</td><td>遥调值</td><td>其他</td></tr></tbody>
                                    </table>
                                </div>
                            </div>*@
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section Styles {
    @Styles.Render("~/plugins/laddaStyles")
    @Styles.Render("~/plugins/sweetAlertStyles") 
}

@section Scripts {
    @Scripts.Render("~/plugins/ladda")
    @Scripts.Render("~/plugins/sweetAlert")
    <script type="text/javascript">
        var fixedsensor = $('#fixedsensor');
        var SensorList = new Array();
        $(document).ready(function () {
            $.ajax({
                url: "/Adjust/FixedSensor",
                method: 'GET',
                data: { wellid: sessionStorage.wellid },
                dataType: 'json',
                async: false,
                success: function (data) {
                    if (data.status == 3) {
                        swal({
                            title: "Welcome in Alerts!",
                            text: "No sensor data is fixed."
                        });
                    }
                    else {
                        $.each(data.data, function (i, item1) {
                            fixedsensor.append('<div class="sidebar-message"><div class="panel panel-primary"><div class="panel-heading">' + item1.SensorChina + '</div><div class="panel-body"><button type="button" class="btn btn-outline btn-primary" id="config-' + item1.SensorName + '" onclick="config(\'' + item1.SensorName + '\')">产品配置信息</button><button type="button" class="btn ladda-button btn-outline btn-warning ladda-button-demo" style="float:right"  data-style="zoom-in" id="adjust-' + item1.SensorName + '" onclick="adjust(\'' + item1.SensorName + '\')">遥调</button></div><table class="table table-hover"><thead><tr><th>参数名</th><th>取值范围</th><th>当前参数值</th><th>遥调值</th><th>其他</th></tr></thead><tbody id="table-' + item1.SensorName + '"></tbody></table></div> </div>')
                            littlediv = $('#table-' + item1.SensorName);
                            $.each(item1.SensorValue, function (j, item2) {
                            //    alert("#" + item2.Address);
                                littlediv.append('<tr><td>' + item2.Name + '</td><td>' + item2.Range + '</td><td><input readOnly="true" id="' + item2.IDCode + '" style ="border-style:none" class="old-' + item1.SensorName + '" value = "' + item2.Value + item2.Unit + '"/></td><td><input id="' + item2.Address + '" class="new-' + item1.SensorName + '" /><p></p></td><td></td></tr>'
                              );
                            })
                            $("input").blur(function () {
                                var value = "dddd";
                                value = $(this).val();
                                var patten = /^[0-9]+(.[0-9]{2})?$/;// /^-?\d+\.?\d*$/;
                                var match1 = patten.test(value);
                                if (match1 == false) {
                                   // $("<div></div>")
                                    $(this).parent().children().last().text("请输入合法数据！样例：1.11");
                                }
                            });
                            $("input").focus(function (){                              
                                $(this).parent().children().last().text("");;
                            });
                        })
                    }
                },
                error: function () {
                    swal({
                        title: "Welcome in Alerts!",
                        text: "The connection between server and units is not very stable. For any technical assistant, please call 1-(517)-927-5910."
                    });

                }
            });
            $.ajax({
                url: "/Adjust/Sensor",
                method: 'GET',
                data: { wellid: sessionStorage.wellid },
                dataType: 'json',
                async: false,
                success: function (data) {
                    if (data.status == 3) {
                        swal({
                            title: "Welcome in Alerts!",
                            text: "No other sensor data available."
                        });
                    }
                    else {
                        $.each(data.data, function (i, item1) {
                            fixedsensor.append('<div class="sidebar-message ' + item1.SensorChina + '" style="display:none"><div class="panel panel-primary"><div class="panel-heading">' + item1.SensorChina + '</div><div class="panel-body"><div style="float:left"><button type="button" class="btn btn-outline btn-primary" id="config-' + item1.SensorName + '" onclick="config(\'' + item1.SensorName + '\')">产品配置信息</button></div><div style="float:left;margin:8px">电池电量剩余百分比：</div><div class="progress" style="width:50%;float:left;margin:7px 0 7px 0"><div class="progress-bar progress-bar-danger progress-bar-striped active" role="progressbar" aria-valuenow="' + item1.Power + '" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em;width:' + item1.Power + '%;">' + item1.Power + '%</div></div><div style="float:right"><button type="button" class="btn ladda-button btn-outline btn-warning ladda-button-demo" style="float:right" data-style="zoom-in" id="adjust-' + item1.SensorName + '" onclick="adjust(\'' + item1.SensorName + '\')">遥调</button></div></div><table class="table table-hover"><thead><tr><th>参数名</th><th>取值范围</th><th>当前参数值</th><th>遥调值</th><th>其他</th></tr></thead><tbody id="table-' + item1.SensorName + '"></tbody></table></div> </div>')
                            littlediv = $('#table-' + item1.SensorName);
                            $.each(item1.SensorValue, function (j, item2) {
                                littlediv.append('<tr><td>' + item2.Name + '</td><td>' + item2.Range + '</td><td><input readOnly="true" id="' + item2.IDCode + '" style ="border-style:none" class="old-' + item1.SensorName + '" value = "' + item2.Value + item2.Unit + '"/></td><td><input id="' + item2.Address + '" class="new-' + item1.SensorName + '" /></td><td></td></tr>'
                                    );
                            });
                            SensorList.push(item1.SensorChina);
                        })
                    }
                    GetSensorList();
                },
                error: function () {
                    swal({
                        title: "Welcome in Alerts!",
                        text: "The connection between server and units is not very stable. For any technical assistant, please call 1-(517)-927-5910."
                    });

                }
            });
        });
        function config(name) {
            sessionStorage.configuration = name;
            window.open("/Adjust/Config", "a", "height=600, width=800, top=50, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
        }
        function adjust(name) {
            var l = $('#adjust-' + name).ladda();
            l.ladda('start');
            var olddata = $('.old-' + name);
            var newdata = $('.new-' + name);
            var adjustdata = "";
            if (name == 'IMPORTANT')
            {
                $.each(newdata, function (i, item) {

                    //var patten = /^-?\d+\.?\d*$/;
                    //var match1 = patten.test(item.Value);


                    //if (match1 == false) {

                    //}
                    //else {
                    //    alert("请字!");
                    //}
                    if (item.value != "" && item.value != olddata[i].value)
                        adjustdata += sessionStorage.wellid + ',' + sessionStorage.username + ',CT_ADJU,' + olddata[i].id + ',' + item.id + ',' + item.value * 100 + '$'
                })
            }
            else {
                //var patten = /^-?\d+\.?\d*$/;
                //var match1 = patten.test(item.Value);


                //if (match1 == false) {

                //}
                //else {
                //    alert("请字!");
                //}
                   
                $.each(newdata, function (i, item) {
                    if (item.value != "" && item.value != olddata[i].value)
                        adjustdata += sessionStorage.wellid + ',' + sessionStorage.username + ',CT_ADJU,' + olddata[i].id + ',' + item.id + ',' + item.value + '$';
                })
            }
            if (adjustdata != "") {
                $.ajax({
                    url: "/Adjust/Adjust",
                    method: 'GET',
                    data: { data: adjustdata },
                    async:true,
                    dataType: 'json',
                    success: function (data) {
                        l.ladda('stop');
                        if (data.status == -1) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "The server connection timed out."
                            });                      
                        }
                        else if (data.status == -2) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "The server connection failed."
                            });
                        }
                        else if (data.status == -3) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "RTU network exception."
                            });
                        }
                        else if (data.status == -4) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "There are too many remote control parameters, please less than 10."
                            });
                        }
                        else if (data.status == -10) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "Sorry, you do not have permission to telepresence!"
                            });
                        }
                        else if (data.status == -11) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "RTU dropped!"
                            });
                        }
                        else if (data.status == -12) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "Remote timeout!"
                            });
                        }
                        else if (data.status == -13) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "Remote control failed!"
                            });
                        }
                        else if (data.status == -14) {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "Desktop program does not allow remote control, please contact the administrator!"
                            });
                        }
                        else {
                            swal({
                                title: "Welcome in Alerts!",
                                text: "Successful remote control parameter "+ data.status +", including "+ data.data
                            });
                            refreshdata(name);
                        }
                    },
                    error: function () {
                        swal({
                            title: "Welcome in Alerts!",
                            text: "The connection between server and units is not very stable. For any technical assistant, please call 1-(517)-927-5910."
                        });

                    }
                });
            }
            else {
                l.ladda('stop'); swal({
                    title: "Welcome in Alerts!",
                    text: "You have not modified any data."
                });

            }
        }
        function showlist()
        {
            $(".checkboxlist").toggle();
            checkbox();
        }
        function checkbox()
        {
            $('input:checkbox').on('change', function () {
                var z = '';
                $('input:checkbox').each(function () {
                    z = this.getAttribute('id');
                    var t = $(this).is(':checked');
                    if (t) {
                        $("." + z).show();
                    }
                    else {
                        $("." + z).hide();
                    }
                });
            })
        }
        function GetSensorList() {
            $(".checkboxlist").empty();
            var j = -1;
            var k = 0;
            $(".checkboxlist").append($("<table id=\"sensorcheckboxlist\" style='width:100%'></table>"));
            for(var i=0;i<SensorList.length;i++)
            {
                if (k % 4 == 0) {
                    j++;
                    $("<tr style='width:100%'></tr>").attr("id", j).appendTo($(".checkboxlist"));
                }
                $("<td style='width:5%'></td>")
                .attr("id", SensorList[i] + "1").appendTo($("#" + j));
                $("<td style='width:15%'></td>")
                .attr("id", SensorList[i] + "2").appendTo($("#" + j));;
                $("<input type=\"checkbox\"></input>")
                .val(SensorList[i])
                .text(SensorList[i])
                .attr("id", SensorList[i])
                //.appendTo($(".checkboxlist"));
                .appendTo($("#" + SensorList[i] + "1"));
                $("<b></b>")
                .val(SensorList[i])
                .text(SensorList[i])
                //.appendTo($(".checkboxlist"));
                .appendTo($("#" + SensorList[i] + "2"));
                $("#" + SensorList[i] + "-btn")
                .attr("name", SensorList[i])
                k++;
            }
        }
        function refreshdata(name) {
            $.ajax({
                url: "/Adjust/RefreshOneSensor",
                method: 'GET',
                data: { wellid: sessionStorage.wellid ,sensorname:name},
                dataType: 'json',
                success: function (data) {
                    if (data.status == 3) {
                        swal({
                            title: "Welcome in Alerts!",
                            text: "No other sensor data available."
                        });
                    }
                    else {
                        fixedsensor.append('<div class="sidebar-message ' + data.data.SensorChina + '" style="display:none"><div class="panel panel-primary"><div class="panel-heading">' + data.data.SensorChina + '</div><div class="panel-body"><div style="float:left"><button type="button" class="btn btn-outline btn-primary" id="config-' + data.data.SensorName + '" onclick="config(\'' + data.data.SensorName + '\')">产品配置信息</button></div><div style="float:left;margin:8px">电池电量剩余百分比：</div><div class="progress" style="width:50%;float:left;margin:7px 0 7px 0"><div class="progress-bar progress-bar-danger progress-bar-striped active" role="progressbar" aria-valuenow="' + data.data.Power + '" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em;width:' + data.data.Power + '%;">' + data.data.Power + '%</div></div><div style="float:right"><button type="button" class="btn ladda-button btn-outline btn-warning ladda-button-demo" style="float:right" data-style="zoom-in" id="adjust-' + data.data.SensorName + '" onclick="adjust(\'' + data.data.SensorName + '\')">遥调</button></div></div><table class="table table-hover"><thead><tr><th>参数名</th><th>取值范围</th><th>当前参数值</th><th>遥调值</th><th>其他</th></tr></thead><tbody id="table-' + data.data.SensorName + '"></tbody></table></div> </div>')
                        littlediv = $('#table-' + data.data.SensorName);
                        littlediv.empty();
                        $.each(data.data.SensorValue, function (j, item2) {
                            littlediv.append('<tr><td>' + item2.Name + '</td><td>' + item2.Range + '</td><td><input readOnly="true" id="' + item2.IDCode + '" style ="border-style:none" class="old-' + data.data.SensorName + '" value = "' + item2.Value + item2.Unit + '"/></td><td><input id="' + item2.Address + '" class="new-' + data.data.SensorName + '" /></td><td></td></tr>'
                                );
                        });
                    }
                },
                error: function () {
                    swal({
                        title: "Welcome in Alerts!",
                        text: "The connection between server and units is not very stable. For any technical assistant, please call 1-(517)-927-5910."
                    });

                }
            });
        }
    </script>
}



